package com.hi.hiparty.ui.fragment;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.hi.hiparty.R;
import com.hi.hiparty.util.ApiHelper;
import com.hi.hiparty.util.ImageDownloader;
import com.hi.hiparty.util.ImageDownloader.ImageViewDownloadedCallBack;

public class PartyThemeListFragment extends ListFragment {

	private static final String LOG_TAG = "PartyThemeListFragment";

	private OnThemeSelectedListener onThemeSelectedListener;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setListAdapter(new ThemeImageAdapter(getActivity()));
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		String theme = (String) l.getItemAtPosition(position);
		onThemeSelectedListener.onThemeSelected(theme);
	}

	class LoadThemeListTask extends AsyncTask<String, Void, Boolean> {

		@Override
		protected Boolean doInBackground(String... params) {
			// TODO Auto-generated method stub
			return null;
		}

	}

	static class ThemeImageAdapter extends BaseAdapter {

		private LayoutInflater mInflater;

		public ThemeImageAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		private static final String[] theme = { "theme_001", "theme_002", "theme_003", "theme_004", "theme_005" };

		private final ImageDownloader imageDownloader = new ImageDownloader();

		public int getCount() {
			return theme.length;
		}

		public String getItem(int position) {
			return theme[position];
		}

		public long getItemId(int position) {
			return theme[position].hashCode();
		}

		public View getView(int position, View view, ViewGroup parent) {
			if (view == null) {
				view = mInflater.inflate(R.layout.party_theme_picker_list_item, null);
			}
			ImageView imageView = (ImageView) view.findViewById(R.id.image);

			ImageViewDownloadedCallBack callback = new ImageViewDownloadedCallBack() {

				public void onImageDownloaded(ImageView imageView, Bitmap bitmap, Map<String, Object> callbackParams) {
					if (null != callbackParams.get("parentView")) {
						View parentView = ((WeakReference<View>) callbackParams.get("parentView")).get();
						if (null != parentView) {
							ProgressBar bar = (ProgressBar) parentView.findViewById(R.id.progress_bar);
							bar.setProgress(100);
							bar.setVisibility(View.GONE);
						}
					}
				}

			};

			Map<String, Object> callbackParams = new HashMap<String, Object>();
			callbackParams.put("parentView", new WeakReference<View>(view));
			String url = ApiHelper.IMAGE_FOLDER + theme[position] + ".jpg";
			imageDownloader.download(url, imageView, callback, callbackParams);
			return view;
		}

		public ImageDownloader getImageDownloader() {
			return imageDownloader;
		}
	}

	public void setOnThemeSelectedListener(OnThemeSelectedListener listener) {
		this.onThemeSelectedListener = listener;
	}

	public static abstract interface OnThemeSelectedListener {

		public void onThemeSelected(String themeid);

	}
}
